package com.xx.springboot.DataStructure.LeetCode.LinkedList;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 *
 *  输入: 1->2->6->3->4->5->6, val = 6
 *  输出: 1->2->3->4->5
 */
public class removeLinkedListElements {

    public static ListNode removeElements(ListNode head, int val) {

        while (head != null && head.val == val){
            ListNode delNode = head;
            head = head.next;
            delNode.next = null;
        }

        if(head == null)
            return head;

        ListNode prev = head;
        while (prev.next != null){
            if(prev.next.val == val){
                ListNode removeNode = prev.next;
                prev.next = removeNode.next;
                removeNode.next = null;
            }else {
                prev = prev.next;
            }
        }
        return head;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        ListNode node = new ListNode(arr);
        System.out.println(node);
        System.out.println(removeLinkedListElements.removeElements(node, 6));
    }


}
